package io.github.wanggit.antrpc.client.monitor.report;

import io.github.wanggit.antrpc.commons.bean.RpcCallLog;
import io.github.wanggit.antrpc.commons.config.IConfiguration;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public abstract class AbstractLogReporter implements ILogReporter {

    private final IConfiguration configuration;

    protected AbstractLogReporter(IConfiguration configuration) {
        this.configuration = configuration;
    }

    @Override
    public void report(RpcCallLog callLog, ZipkinAllMethodTracing.TracingPairs lastTracingPairs) {
        if (!configuration.getCallLogReporterConfig().isReportArgumentValues()) {
            callLog.setRequestArgs(null);
        }
        callLog.setAppName(configuration.getApplicationName());
        callLog.setIp(configuration.getExposeIp());
        callLog.setPort(configuration.getPort());
        callLog.setDate(new Date().toInstant().toString());
        try {
            internalReport(callLog, lastTracingPairs);
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error("Report Log ERROR. ", e);
            }
        }
    }

    protected abstract void internalReport(
            RpcCallLog log, ZipkinAllMethodTracing.TracingPairs lastTracingPairs);
}
